﻿using System;
using System.Linq;
using Application.App;
using Domain.Types;
using DTO;
using FacadeApi.Extension;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class WalletController : ControllerBase
    {
        public PayApplication payApplication { get; set; }
        public WalletApplication walletApplication { get; set; }
        public ILogger<WalletController> _logger { get; set; }


        /// <summary>
        /// 个人中心用户钱包信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<UserBlanceInfoOutput> GetUserBlanceInfo()
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = walletApplication.GetUserBlanceInfo(int.Parse(uid));

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }
        /// <summary>
        /// 今日到账 待审核金额
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<UserBlanceInfoSumOutput> GetUserBlanceInfoSum()
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = walletApplication.GetUserBlanceInfoSum(int.Parse(uid));

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }
        /// <summary>
        /// 钱包详情
        /// </summary>
        /// <param name="type">{收入钱包:0,发布钱包:1}</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<UserBlanceInfoSumOutput> GetUserBlanceDetails(int type, int page = 1, int size = 20)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = walletApplication.GetUserBlanceDetails(int.Parse(uid), type, page,size);

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }
        /// <summary>
        /// 提现手续费，文字
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<UserBlanceInfoSumOutput> GetUserWithDrawDetails()
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = walletApplication.GetUserWithDrawDetails(int.Parse(uid));

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }

        /// <summary>
        /// 获取支付参数
        /// </summary>
        /// <param name="payMoneyInput"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult CreatePayInfo(PayMoneyInput payMoneyInput)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var token = payApplication.CreatePayInfo(int.Parse(uid), payMoneyInput);

                return this.Success(token);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }



        /// <summary>
        /// 冲值保证金
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult ChargeMarginBlance([FromForm] int money)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var b = walletApplication.ChargeMarginBlance(int.Parse(uid), money);

                return this.Success(b);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 提现
        /// </summary>
        /// <param name="from">0:用户余额 1:发布余额</param>
        /// <param name="payChannel">0:支付宝 1:微信</param>
        /// <param name="money">提现金额</param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult WithDrawMoney([FromForm] int from, [FromForm] PayChannelEnum payChannel, [FromForm] int money)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                if (from == 1)
                {
                    var r = walletApplication.WithDrawMoneyByCharge(int.Parse(uid), payChannel, money);
                    return this.Success(new { ReceiveMoney = r.ReceiveMoney, TaxFee = r.TaxFee });
                }
                else
                {
                    var r = walletApplication.WithDrawMoneyByInCome(int.Parse(uid), payChannel, money);
                    return this.Success(new { ReceiveMoney = r.ReceiveMoney, TaxFee = r.TaxFee });
                }
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
        /// <summary>
        /// 设置支付宝账号信息
        /// </summary>
        /// <param name="phoneCode"></param>
        /// <param name="account"></param>
        /// <param name="realName"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult SetAliPayInfo([FromForm] string phoneCode, [FromForm] string account, [FromForm] string realName)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;

                var r = walletApplication.SetAliPayInfo(int.Parse(uid), phoneCode, account, realName);
                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 设置微信账号信息
        /// </summary>
        /// <param name="phoneCode"></param>
        /// <param name="account"></param>
        /// <param name="realName"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult SetWxPayInfo([FromForm] string phoneCode, [FromForm] string account, [FromForm] string realName)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;

                var r = walletApplication.SetWxPayInfo(int.Parse(uid), phoneCode, account, realName);
                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
        /// <summary>
        /// 收入钱包转到发布钱包
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult IncomeToCharge([FromForm] int money)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                if (money == 0) throw new BizException("转移金额不能为0");

                var r = walletApplication.IncomeToCharge(int.Parse(uid), money);
                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }



    }
}
